package Hot100.Easy.LC20;
/**
 * 判断括号的有效性
 */

import java.util.Stack;

public class LC20 {
    public static void main(String[] args) {
        LC20 solution = new LC20();
        String s1="()";
        String s2="(}";
        String s3="()[]{}";
        String s4="([])";

        System.out.println(solution.verifyBrackets(s1));
        System.out.println(solution.verifyBrackets(s2));
        System.out.println(solution.verifyBrackets(s3));
        System.out.println(solution.verifyBrackets(s4));


    }

    public boolean verifyBrackets(String s) {
        int n=s.length();
        if(n%2!=0) return false;
        Stack<Character> stack = new Stack<>();
        for(char c:s.toCharArray()){
            if(c=='('||c=='['||c=='{')
            {
                stack.push(c);
            }
            else{
                if(stack.isEmpty()){
                    return false;
                }
                char top=stack.pop();
                if(c==')' && top!='('||c==']' && top!='['||c=='}'&&top!='{'){
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
